//
//  HEConsulCommentViewController.m
//  HealthElderly
//
//  Created by 吴欧 on 2020/12/29.
//  Copyright © 2020 honglutec. All rights reserved.
//

#import "HEConsulCommentViewController.h"
#import "HDRatingStarView.h"

@interface HEConsulCommentViewController ()

@property (nonatomic, strong) UIScrollView *scrollView;
// 约束参照视图,也是容器视图
@property (nonatomic, strong) UIView *contentView;

@property (nonatomic, strong) UILabel *titleLb;
@property (nonatomic, strong) UILabel *satisfieLb;
@property (nonatomic, strong) QMUITextView *textView;
@property (nonatomic, strong) HDRatingStarView *startView;
@property (nonatomic, assign) NSInteger currentScore;

@end

@implementation HEConsulCommentViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.title = @"服务评价";
    
    [self initUI];
}

- (void)initUI {
    
    // 1. 添加scrollView
    [self.view addSubview:self.scrollView];
    // 2. 添加参照视图
    [self.scrollView addSubview:self.contentView];
    
    // 头像
    UIImageView *iconView = [UIImageView new];
    [iconView sd_setImageWithURL:[NSURL URLWithString: _avatar] placeholderImage:IMAGE_NAMED(@"hmang")];
    iconView.contentMode = UIViewContentModeScaleAspectFill;
    [iconView hg_setAllCornerWithCornerRadius: 40];
    [self.contentView addSubview:iconView];
    
    // 服务人员名称
    UILabel *nameLb = [UILabel new];
    nameLb.text = _doc_name;
    nameLb.font = DYBlodFont(18);
    nameLb.textColor = CNavLightBlackColor;
    nameLb.textAlignment = NSTextAlignmentCenter;
    [self.contentView addSubview: nameLb];
     
    _titleLb = [UILabel new];
    _titleLb.text = @"请您对本次服务做出评价";
    _titleLb.font = DYFont(16);
    _titleLb.textColor = CNavLightBlackColor;
    _titleLb.textAlignment = NSTextAlignmentCenter;
    [_contentView addSubview: _titleLb];
    
    _satisfieLb = [UILabel new];
    _satisfieLb.text = @"非常满意";
    _satisfieLb.font = DYBlodFont(23);
    _satisfieLb.textAlignment = NSTextAlignmentCenter;
    [_contentView addSubview: _satisfieLb];
    
    [iconView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(_contentView).offset(40);
        make.centerX.equalTo(self.contentView);
        make.width.height.mas_offset(80);
    }];
    
    [nameLb mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(iconView.mas_bottom).offset(20);
        make.left.equalTo(_contentView).mas_offset(15);
        make.right.equalTo(_contentView).mas_offset(-15);
    }];
 
    [_titleLb mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(nameLb.mas_bottom).offset(20);
        make.left.equalTo(_contentView).offset(30);
        make.right.equalTo(_contentView).offset(-30);
    }];
    
    [_satisfieLb mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(_titleLb.mas_bottom).offset(20);
        make.left.right.equalTo(_contentView);
    }];
 
    _currentScore = 5;
    
    // 星星评分
    _startView = [[HDRatingStarView alloc] init];
    _startView.defaultColor = KHexColor(@"CDCDCD");
    _startView.renderColor = KHexColor(@"F67605");
    _startView.allowTouchToSelectScore = YES;
    _startView.score = 5;
    _startView.fullScore = 5;
    _startView.starWidth = 30;
    _startView.countForOneStar = 1;
    _startView.starImage = IMAGE_NAMED(@"rating_sel");
    [self.contentView addSubview:_startView];
    
    [_startView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(_satisfieLb.mas_bottom).mas_offset(20);
        make.width.offset(30 * 5 + 5 * 4);
        make.centerX.equalTo(_contentView);
        make.height.mas_greaterThanOrEqualTo(30);
    }];
     
    NSArray *arr = @[@"差", @"较差", @"一般", @"满意", @"非常满意"];
    [_startView returnScoreHandler:^(CGFloat score) {
        NSInteger index = score;
        self.currentScore = index;
        if (score == 0) {
            self.satisfieLb.text = arr[0];
        }else {
            self.satisfieLb.text = arr[index-1];
        }
    }];
    
    // 描述
    _textView = [QMUITextView new];
    // 限制可输入的字符长度
    self.textView.maximumTextLength = 1500;
    self.textView.placeholder = @"服务过程还满意吗？说说您的感受和建议吧";
    self.textView.placeholderColor = CNavDargGrayColor; // 自定义 placeholder 的颜色
    self.textView.textContainerInset = UIEdgeInsetsMake(5, 7, 5, 7);
    self.textView.typingAttributes = @{NSFontAttributeName: DYFont(17),
                                       NSParagraphStyleAttributeName: [NSMutableParagraphStyle qmui_paragraphStyleWithLineHeight:20]
    };
    _textView.backgroundColor = CSplitLineColor;
    _textView.delegate = self;
    [self.contentView addSubview:_textView];
    self.textView.layer.cornerRadius = 10;
    
 
    [_textView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(_startView.mas_bottom).mas_offset(30);
        make.left.equalTo(_contentView).mas_offset(30);
        make.right.equalTo(_contentView).mas_offset(-30);
        make.height.mas_offset(200);
    }];
    
 
    // 提交
    QMUIFillButton *agreeBtn = [[QMUIFillButton alloc] initWithFillColor:CNavBgColor titleTextColor:CNavBgFontColor];
    agreeBtn.titleLabel.font = UIFontMake(16);
    [agreeBtn setTitle:@"提交" forState:UIControlStateNormal];
    [self.view addSubview:agreeBtn];
    [agreeBtn addTarget:self action:@selector(commitReq) forControlEvents:UIControlEventTouchUpInside];

    [agreeBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(_textView.mas_bottom).mas_offset(30);
        make.centerX.equalTo(_contentView);
        make.left.equalTo(_contentView).mas_offset(30);
        make.right.equalTo(_contentView).mas_offset(-30);
        make.height.mas_offset(40);
        make.bottom.equalTo(_contentView).mas_offset(-30);
    }];
}
 

- (void)viewDidLayoutSubviews {
    [super viewDidLayoutSubviews];
    // 布局子控件
    // 设置scrollView约束
    [self.scrollView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(self.view).insets(UIEdgeInsetsMake(ZXNavBarHeight, 0, 0, 0));
    }];
    
    // 设置参照视图的约束
    [self.contentView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(self.scrollView);
        make.width.equalTo(self.scrollView);
//        make.height.greaterThanOrEqualTo(@0.0f);
    }];
}

- (NSNumber *)getScore{
    NSArray *arr = @[@10, @20, @40, @80, @100];
    return arr[self.currentScore-1];
}

- (void)commitReq {
    
    NSString *cnt = _textView.text.qmui_trim;
    
    if ([DYTools isBlankString:cnt]) {
        cnt = @"这个用户很懒,什么都没留下";
    }
    
    NSMutableDictionary *dict = [NSMutableDictionary dictionary];
    NSString *urlStr = @"";
    if (_type == HEConsulCommentTypeSickbed) {
        urlStr = KcommentServer;
        [dict setValue:_usid forKey:@"usid"];
        [dict setValue:_order_id forKey:@"orderNo"];
        [dict setValue:[NSNumber numberWithInteger:self.currentScore] forKey:@"score"];
        [dict setValue:cnt forKey:@"content"];
    }else {
        urlStr = KcommentSave;
        [dict setValue:_target_id forKey:@"target_id"];
        [dict setValue:[NSString stringWithFormat:@"%ld", _type] forKey:@"type"];
        [dict setValue:_out_inst_id forKey:@"out_inst_id"];
        [dict setValue:cnt forKey:@"content"];
        [dict setValue:[NSNumber numberWithInteger:self.currentScore] forKey:@"score"];
    }
 
    [WOAFNetWorkingHandle POSTWithBaseUrl:KServerUrl suffixUrl:urlStr parameters: dict success:^(id  _Nonnull responseObject) {
        NSLog(@"response：  %@", [responseObject mj_JSONString]);
        HEResponse *response = [HEResponse mj_objectWithKeyValues:responseObject];
     
        if (response.success) {
            [Dialog toastBottom:@"评价成功"];
            if (self.backParamBlock) {
                self.backParamBlock(dict[@"score"]);
            }
            [self.navigationController popViewControllerAnimated:YES];
        }
    } failure:^(NSError * _Nonnull error) {
    }];
}


#pragma mark - getters
// scrollView
- (UIScrollView *)scrollView {
    if (_scrollView == nil) {
        _scrollView = [[UIScrollView alloc] init];
        _scrollView.backgroundColor = KWhiteColor;
        _scrollView.alwaysBounceVertical = YES;
    }
    return _scrollView;
}

// 约束参照视图
- (UIView *)contentView {
    if (_contentView == nil) {
        _contentView = [[UIView alloc] init];
        _contentView.backgroundColor = KWhiteColor;
    }
    return _contentView;
}

@end
